﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class SearchPatient : Form
    {
        //Creates the variables to hold the information for the current patient
        //to be registered. 
        private String currentPatientFirstName;
        private String currentPatientLastName;
        private String currentPatientBirthDate;
        private Validation validate = new Validation();
        private Decryption key = new Decryption();

        private String MyConString;

        public SearchPatient()
        {
            InitializeComponent();
            MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
        }


        /// <summary>
        ///Searchs the data base for the patient. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Gets all the information for the patient from the text.
                currentPatientFirstName = patientFirstNameText.Text;
                currentPatientLastName = patientLastNameText.Text;
                currentPatientBirthDate = patientBirthdateText.Text;

                //Validates if the first name check box is clicked.
                if (firstNameCheckBox.Checked == true)
                {
                    validate.validateName(currentPatientFirstName, "First");
                }
                //Validates if the last name check box is clicked.
                if (lastNameCheckBox.Checked == true)
                {
                    validate.validateName(currentPatientLastName, "Last");
                }
                //Validates if the date of birth check box is clicked.
                if (dobCheckBox.Checked == true)
                {
                    validate.validateDate(currentPatientBirthDate);
                }

                //If just searching by firstname
                if (firstNameCheckBox.Checked == true && lastNameCheckBox.Checked == false && dobCheckBox.Checked == false)
                {
                    executeCommandString("SELECT * FROM `PATIENT` WHERE firstName='" + currentPatientFirstName + "';");
                }

                //If just searching by lastname
                if (firstNameCheckBox.Checked == false && lastNameCheckBox.Checked == true && dobCheckBox.Checked == false)
                {
                    executeCommandString("SELECT * FROM `PATIENT` WHERE lastName='" + currentPatientLastName + "';");
                }

                //If just searching by DOB
                if (firstNameCheckBox.Checked == false && lastNameCheckBox.Checked == false && dobCheckBox.Checked == true)
                {
                    executeCommandString("SELECT * FROM `PATIENT` WHERE dateOfBirth='" + currentPatientBirthDate + "';");
                }

                //If searching by firstname, lastname, and dob
                if(firstNameCheckBox.Checked == true && lastNameCheckBox.Checked == true && dobCheckBox.Checked == true)
                {
                    executeCommandString("SELECT * FROM `PATIENT` WHERE firstName='"
                    + currentPatientFirstName + "' AND lastName='" + currentPatientLastName + "' AND dateOfBirth='" + currentPatientBirthDate + "';");  
                }
                 
                //if searching by firstname, and lastname
                else if (firstNameCheckBox.Checked == true && lastNameCheckBox.Checked == true && dobCheckBox.Checked == false)
                {

                    executeCommandString("SELECT * FROM `PATIENT` WHERE firstName='"
                                                    + currentPatientFirstName + "' AND lastName='" + currentPatientLastName + "';");
                }
                
                //if searching by lastname, and dob
                else if (firstNameCheckBox.Checked == false && lastNameCheckBox.Checked == true && dobCheckBox.Checked == true)
                {

                    executeCommandString("SELECT * FROM `PATIENT` WHERE lastName='" + currentPatientLastName + "' AND dateOfBirth='" + currentPatientBirthDate + "';");
                }
                
                //if searching by firstname, and dob
                else if (firstNameCheckBox.Checked == true && lastNameCheckBox.Checked == false && dobCheckBox.Checked == true)
                {

                    executeCommandString("SELECT * FROM `PATIENT` WHERE firstName='" + currentPatientFirstName + "' AND dateOfBirth='" + currentPatientBirthDate + "';");
                }
                
                //if no search parameters are selected
                else if (firstNameCheckBox.Checked == false && lastNameCheckBox.Checked == false && dobCheckBox.Checked == false)
                {
                    MessageBox.Show("Please use the checkboxes to select search parameters.");
                }
   
            }
            
            //Shows the exceptions if the validation class catches a format error.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
        }

        // <summary>
        //  Excecutes command lines and displays the results to the gridview.
        // </summary>
        // <param name="commandLine">Select statement to be executed.</param>
        private void executeCommandString(string commandLine)
        {
            //Holds the values to be stored into the dataGridview to be displayed.
            List<StringHolder> list = new List<StringHolder>();

            OdbcConnection MyConnection = new OdbcConnection(MyConString);

            MyConnection.Open();

            OdbcCommand MyCommand = new OdbcCommand(commandLine, MyConnection);
            OdbcDataReader MyDataReader;
            MyDataReader = MyCommand.ExecuteReader();

            
            while (MyDataReader.Read())
            {
                //Creates a string to hold the values from the query.
                String builder = " ";
                //Checks the number of columns that are going to be readin, and for each column.
                int values = MyDataReader.VisibleFieldCount;
                for (int currentColumn = 0; currentColumn < values; currentColumn++)
                {
                    //If the value in the current column is null.
                    if (MyDataReader.IsDBNull(currentColumn) == true)
                    {
                        builder += " " + MyDataReader.GetName(currentColumn) + " : " + " NULL ";
                    }
                    else
                    {
                        //Checks for the type of data in the column, and handles it appropriately.
                        //For each it checks the type, gets the column name, and then the value.
                        String type = MyDataReader.GetDataTypeName(currentColumn);

                        if (type == "varchar")
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetString(currentColumn).ToString() + " ";
                        }
                        if (type == "integer")
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetInt32(currentColumn).ToString() + " ";
                        }
                        if (type == "decimal")
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDecimal(currentColumn).ToString() + " ";
                        }
                        if (type == "date")
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDate(currentColumn).ToShortDateString() + " ";
                        }
                    }
                }
                //Stores the string in the string wrapper.
                StringHolder currentString = new StringHolder(builder);
                //Adds the string to be added into the data grid view.
                list.Add(currentString);
            }
            //Displays the data in the datagridview.
            dataDisplay.DataSource = list;
            dataDisplay.Show();

            //Close all resources
            MyDataReader.Close();
            MyConnection.Close();
        }
     
    }
}
